package cn.com.woxitech.dev.controller.api;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;

import cn.com.woxitech.dev.common.APIResultFactory;
import cn.com.woxitech.dev.common.Const;
import cn.com.woxitech.dev.common.HttpClientUtils;
import cn.com.woxitech.dev.common.JSONUtils;
import cn.com.woxitech.dev.common.QiniuPhotoUploadUtils;
import cn.com.woxitech.dev.model.Lecture;
import cn.com.woxitech.dev.model.QiniuUploadResult;
import cn.com.woxitech.dev.model.RoomManager;
import cn.com.woxitech.dev.model.WmChathistoryFetch;
import cn.com.woxitech.dev.model.gen.WmApp;
import cn.com.woxitech.dev.model.gen.WmCatalog;
import cn.com.woxitech.dev.model.gen.WmForm;
import cn.com.woxitech.dev.model.gen.WmFormField;
import cn.com.woxitech.dev.model.gen.WmFormRecord;
import cn.com.woxitech.dev.model.gen.WmFormRoom;
import cn.com.woxitech.dev.model.gen.WmLecture;
import cn.com.woxitech.dev.model.gen.WmLecturePage;
import cn.com.woxitech.dev.model.gen.WmRoom;
import cn.com.woxitech.dev.model.gen.WmUser;
import cn.com.woxitech.dev.service.AppService;
import cn.com.woxitech.dev.service.CatalogService;
import cn.com.woxitech.dev.service.FormService;
import cn.com.woxitech.dev.service.LecturePageService;
import cn.com.woxitech.dev.service.LectureService;
import cn.com.woxitech.dev.service.OrganizationService;
import cn.com.woxitech.dev.service.QiniuService;
import cn.com.woxitech.dev.service.RoomService;
import cn.com.woxitech.dev.service.SMSCodeService;
import cn.com.woxitech.dev.service.UserService;

@RestController
@RequestMapping("/luyan")
public class FormLUYANController {
	
	@Autowired
	FormService formService;
	
	@Autowired
	UserService userService;
	
	@Autowired
	RoomService roomService;
	
	@Autowired
	private Environment env;


	@RequestMapping(value="/form/list",method=RequestMethod.POST)
	public Object form_list(@RequestBody String body) {
		JsonNode jsonNode;
		Map<String, Object> mp = new HashMap<String, Object>();
		try {
			jsonNode = JSONUtils.toJson(body);

			List<WmForm> list = formService.getAllForms();
			mp.put("forms", list);
			return APIResultFactory.success(mp);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return APIResultFactory.fail();
		}
		
	}
	

	@RequestMapping(value="/form/room/{roomid}/enrollcheck/custom_1",method=RequestMethod.POST)
	public Object form_room_check_custom_1(@PathVariable String roomid, @RequestBody String body) {
		JsonNode jsonNode;
		Map<String, Object> mp = new HashMap<String, Object>();
		try {
			jsonNode = JSONUtils.toJson(body);
			String enrolltel = jsonNode.get("enrolltel").asText();
			String enrollemail = jsonNode.get("enrollemail").asText();
			String useruuid = jsonNode.get("useruuid").asText();
			
			String wmFormId = formService.getFormIdByRoomId(roomid);
			if(wmFormId == null){
				
				mp.put("next", false);
			}else{
				
				WmFormRecord r1 = formService.getFormRecordByFieldValue(wmFormId, enrolltel);
				WmFormRecord r2 = formService.getFormRecordByFieldValue(wmFormId, enrollemail);
				
				if(r1 != null && r2!=null && r1.getRecordid().equals(r2.getRecordid())){
					
	//				List<WmFormRecord> records = formService.getFormRecordByRecordId(r1.getRecordid());
					//关联useruuid与record
					WmUser user = userService.getUserByUuid(useruuid);
					user.setTel(enrolltel);
					userService.updateUser(user);
					
					mp.put("next", true);
				}else{
					mp.put("next", false);
				}
			}
			return APIResultFactory.success(mp);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return APIResultFactory.fail();
		}
		
	}
	
	@RequestMapping(value="/form/room/{roomid}/enrollcheck/custom_2",method=RequestMethod.POST)
	public Object form_room_check_custom_2(@PathVariable String roomid, @RequestBody String body) {
		JsonNode jsonNode;
		Map<String, Object> mp = new HashMap<String, Object>();
		try {
			jsonNode = JSONUtils.toJson(body);
			
			String name = jsonNode.get("name").asText();
			String company = jsonNode.get("company").asText();
			String position = jsonNode.get("position").asText();
			String tel = jsonNode.get("tel").asText();
			String useruuid = jsonNode.get("useruuid").asText();
			
			String wmFormId = formService.getFormIdByRoomId(roomid);
			if(wmFormId == null){
				
				mp.put("next", false);
			}else{
				
//				WmFormRecord r = formService.getFormRecordByFieldValue(wmFormId, useruuid);
				
				String recordid = UUID.randomUUID().toString();
				
				Date now = new Date(System.currentTimeMillis());
				
				WmFormRecord r1 = new WmFormRecord();
//				2d88f77f-88d8-4fe9-af0d-2b56c5a765eb 姓名
				r1.setFieldid("2d88f77f-88d8-4fe9-af0d-2b56c5a765eb");
				r1.setFieldvalue(name);
				r1.setFormid(wmFormId);
				r1.setRecordid(recordid);
				r1.setTime(now);
				
				WmFormRecord r2 = new WmFormRecord();
//				586a7259-bf01-45dd-b3e7-e78ca474c2b7 单位
				r2.setFieldid("586a7259-bf01-45dd-b3e7-e78ca474c2b7");
				r2.setFieldvalue(company);
				r2.setFormid(wmFormId);
				r2.setRecordid(recordid);
				r2.setTime(now);
				
				
				WmFormRecord r3 = new WmFormRecord();
//				f583dff9-a970-473a-8451-d8209310b4a9 职务
				r3.setFieldid("f583dff9-a970-473a-8451-d8209310b4a9");
				r3.setFieldvalue(position);
				r3.setFormid(wmFormId);
				r3.setRecordid(recordid);
				r3.setTime(now);
				
				WmFormRecord r4 = new WmFormRecord();
//				4e05d5c0-80c4-407c-a9f0-fe8164ac882a 电话
				r4.setFieldid("4e05d5c0-80c4-407c-a9f0-fe8164ac882a");
				r4.setFieldvalue(tel);
				r4.setFormid(wmFormId);
				r4.setRecordid(recordid);
				r4.setTime(now);
				
				WmFormRecord r5 = new WmFormRecord();
//				98fb9d43-4296-49e2-9860-0f496155e25d uuid				
				r5.setFieldid("98fb9d43-4296-49e2-9860-0f496155e25d");
				r5.setFieldvalue(useruuid);
				r5.setFormid(wmFormId);
				r5.setRecordid(recordid);
				r5.setTime(now);
					
				
				this.formService.saveRecord(r1);
				this.formService.saveRecord(r2);
				this.formService.saveRecord(r3);
				this.formService.saveRecord(r4);
				this.formService.saveRecord(r5);
				
				mp.put("next", true);
			}
			return APIResultFactory.success(mp);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return APIResultFactory.fail();
		}
		
	}

	@RequestMapping(value="/form/room/{roomid}/user/{userid}/autocheck_2",method=RequestMethod.POST)
	public Object form_room_autocheck_2(@PathVariable String roomid,@PathVariable String userid, @RequestBody String body) {
		JsonNode jsonNode;
		Map<String, Object> mp = new HashMap<String, Object>();
		try {
//			jsonNode = JSONUtils.toJson(body);
			String formid = formService.getFormIdByRoomId(roomid);
			WmUser user = userService.getUserByUuid(userid);
			
			if(formid == null ){
				mp.put("next", true);	
			}else{
				WmFormRecord r1 = formService.getFormRecordByFieldValue(formid, user.getUuid());
				if(r1 !=null){
					mp.put("next", true);
				}else{
					mp.put("next", false);
				}
			}
			
			return APIResultFactory.success(mp);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return APIResultFactory.fail();
		}
		
	}
	@RequestMapping(value="/form/room/{roomid}/user/{userid}/autocheck",method=RequestMethod.POST)
	public Object form_room_autocheck(@PathVariable String roomid,@PathVariable String userid, @RequestBody String body) {
		JsonNode jsonNode;
		Map<String, Object> mp = new HashMap<String, Object>();
		try {
//			jsonNode = JSONUtils.toJson(body);
			String formid = formService.getFormIdByRoomId(roomid);
			WmUser user = userService.getUserByUuid(userid);
			
			if(formid == null ){
				mp.put("next", true);	
			}else{
				WmFormRecord r1 = formService.getFormRecordByFieldValue(formid, user.getTel());
				if(r1 !=null){
					mp.put("next", true);
				}else{
					mp.put("next", false);
				}
			}
			
			return APIResultFactory.success(mp);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return APIResultFactory.fail();
		}
		
	}


	@RequestMapping(value="/form/{formid}/room_list",method=RequestMethod.POST)
	public Object form_room_list(@PathVariable String formid,@RequestBody String body) {
		JsonNode jsonNode;
		Map<String, Object> mp = new HashMap<String, Object>();
		
		try {
//			jsonNode = JSONUtils.toJson(body);
		
			List<WmFormRoom> list = formService.getFormRoomListByFormid(formid);
			
			List<String> roomuuidStrs = new ArrayList<String>();
			
			for(WmFormRoom wfr : list){
				roomuuidStrs.add(wfr.getRoomid());
			}
			List<WmRoom> rooms = new ArrayList<WmRoom>();
			if(!roomuuidStrs.isEmpty()){
				rooms = roomService.getRoomsByUuidList(roomuuidStrs, "time");				
			}
			
			mp.put("pos", rooms.size());
			mp.put("rooms", rooms);
			
			mp.put("next", true);

			return APIResultFactory.success(mp);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			mp.put("next", false);
			mp.put("rooms", new ArrayList<WmRoom>());
			return APIResultFactory.success(mp);
		}
		
	}
	
	

	@RequestMapping(value="/form/{formid}/field_list",method=RequestMethod.POST)
	public Object form_field_list(@PathVariable String formid,@RequestBody String body) {
		JsonNode jsonNode;
		Map<String, Object> mp = new HashMap<String, Object>();
		
		try {
//			jsonNode = JSONUtils.toJson(body);
		
			List<WmFormField> list = formService.getFormFieldListByFormid(formid);
			
			mp.put("fields", list);
			
			mp.put("next", true);

			return APIResultFactory.success(mp);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			mp.put("next", false);
			mp.put("fields", new ArrayList<WmFormField>());
			return APIResultFactory.success(mp);
		}
		
	}
	

	@RequestMapping(value="/form/{formid}/room/{roomid}/bind",method=RequestMethod.POST)
	public Object form_room_bind(@PathVariable String formid,@PathVariable String roomid, @RequestBody String body) {
		JsonNode jsonNode;
		Map<String, Object> mp = new HashMap<String, Object>();
		try {
//			jsonNode = JSONUtils.toJson(body);
			
			if(formid != null && roomid != null){
				formService.bindFormRoomById(formid, roomid);		
				mp.put("next", true);
			}else{
				mp.put("next", false);	
			}

			return APIResultFactory.success(mp);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return APIResultFactory.fail();
		}
		
	}
	@RequestMapping(value="/form/{formid}/room/{roomid}/unbind",method=RequestMethod.POST)
	public Object form_room_unbind(@PathVariable String formid,@PathVariable String roomid, @RequestBody String body) {
		JsonNode jsonNode;
		Map<String, Object> mp = new HashMap<String, Object>();
		try {
//			jsonNode = JSONUtils.toJson(body);
			
			if(formid != null && roomid != null){
				formService.unbindFormRoomById(formid, roomid);		
				mp.put("next", true);
			}else{
				mp.put("next", false);	
			}

			return APIResultFactory.success(mp);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return APIResultFactory.fail();
		}
		
	}
	

	@RequestMapping(value="/form/{formid}/record/{recordid}/delete",method=RequestMethod.POST)
	public Object form_record_delete(@PathVariable String formid,@PathVariable String recordid, @RequestBody String body) {
		JsonNode jsonNode;
		Map<String, Object> mp = new HashMap<String, Object>();
		try {

			formService.deleteFormRecord(formid , recordid);
			mp.put("next", true);			
			return APIResultFactory.success(mp);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			mp.put("next", false);
			return APIResultFactory.success(mp);
		}
		
	}

	@RequestMapping(value="/form/{formid}/records",method=RequestMethod.POST)
	public Object form_records(@PathVariable String formid, @RequestBody String body) {
		JsonNode jsonNode;
		Map<String, Object> mp = new HashMap<String, Object>();
		try {
			jsonNode = JSONUtils.toJson(body);
			List<WmFormField> fieldsList = formService.getAllFieldsByFormid(formid);
			List<List<WmFormRecord>> recordsAll = new ArrayList<List<WmFormRecord>>();
			
			int fieldSize = fieldsList.size();
			int recordsSize = 0;
			for(int i = 0; i< fieldSize; i++){
				List<WmFormRecord> recordsList = formService.getRecordsByFormidAndFieldid(formid,fieldsList.get(i).getFieldid());
				recordsAll.add(recordsList);
				recordsSize = recordsList.size();
			}
			
			
			
			List<Map<String, Object>> records = new ArrayList<Map<String, Object>>();
			for(int i =0 ;i < recordsSize ; i++){
				
				Map<String, Object> rd = new HashMap<String, Object>();
				
				for(int j = 0; j< fieldSize; j++){
					
					rd.put("field"+(j+1), recordsAll.get(j).get(i));
					
				}
				records.add(rd);
			}
			
			mp.put("fields", fieldsList);
			mp.put("records", records);
			
			return APIResultFactory.success(mp);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return APIResultFactory.fail();
		}
		
	}
	
	@RequestMapping(value="/form/{formid}/record/{recordid}/update",method=RequestMethod.POST)
	public Object form_record_update(@PathVariable String formid,@PathVariable String recordid, @RequestBody String body) {
		JsonNode jsonNode;
		Map<String, Object> mp = new HashMap<String, Object>();
		Map<String, String> fieldRecordsMp = new HashMap<String, String>();
		try {
			jsonNode = JSONUtils.toJson(body);
			
//			ArrayNode array = jsonNode.get("record").;
			
			int size = jsonNode.get("record").size();
			for(int i=0;i< size;i++){
				JsonNode el = jsonNode.get("record").get(i);
				fieldRecordsMp.put(el.get("fieldid").asText(), el.get("fieldvalue").asText());
			}
			
			List<WmFormField> list = formService.getAllFieldsByFormid(formid);
			List<WmFormRecord> recordList = new ArrayList<WmFormRecord>();
			Date time = new Date(System.currentTimeMillis());
			for(int i = 0 ; i< list.size(); i++){
				WmFormRecord e = new WmFormRecord();
				e.setFieldid(list.get(i).getFieldid());
				e.setFieldvalue(fieldRecordsMp.get(list.get(i).getFieldid()));
				e.setFormid(formid);
				e.setRecordid(recordid);
				e.setTime(time);
				recordList.add(e);
			}
			formService.updateRecords(recordList);
			
			mp.put("next", true);
			return APIResultFactory.success(mp);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			mp.put("next", false);
			return APIResultFactory.fail();
		}
		
	}

	@RequestMapping(value="/form/{formid}/record/create",method=RequestMethod.POST)
	public Object form_record_create(@PathVariable String formid, @RequestBody String body) {
		JsonNode jsonNode;
		Map<String, Object> mp = new HashMap<String, Object>();
		Map<String, String> fieldRecordsMp = new HashMap<String, String>();
		try {
			jsonNode = JSONUtils.toJson(body);
			
//			ArrayNode array = jsonNode.get("record").;
			
			int size = jsonNode.get("record").size();
			for(int i=0;i< size;i++){
				JsonNode el = jsonNode.get("record").get(i);
				fieldRecordsMp.put(el.get("fieldid").asText(), el.get("fieldvalue").asText());
			}
			
			String recordid = UUID.randomUUID().toString();
			
			List<WmFormField> list = formService.getAllFieldsByFormid(formid);
			List<WmFormRecord> recordList = new ArrayList<WmFormRecord>();
			Date time = new Date(System.currentTimeMillis());
			for(int i = 0 ; i< list.size(); i++){
				WmFormRecord e = new WmFormRecord();
				e.setFieldid(list.get(i).getFieldid());
				e.setFieldvalue(fieldRecordsMp.get(list.get(i).getFieldid()));
				e.setFormid(formid);
				e.setRecordid(recordid);
				e.setTime(time);
				recordList.add(e);
			}
			formService.saveRecords(recordList);
			
			mp.put("next", true);
			return APIResultFactory.success(mp);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			mp.put("next", false);
			return APIResultFactory.fail();
		}
		
	}


//	post: /luyan/form/{formid}/delete
//	body:
//	{
//	name:xxx,
//	rooms:[{roomuuid: roomid1},{roomuuid:roomid2},{roomuuid:roomid3}]
//	}
//
	
	@RequestMapping(value="/form/{formid}/delete",method=RequestMethod.POST)
	public Object form_delete(@PathVariable String formid, @RequestBody String body) {
		JsonNode jsonNode;
		Map<String, Object> mp = new HashMap<String, Object>();
		try {
			jsonNode = JSONUtils.toJson(body);
			
			
			
			WmForm form = formService.getFormByFormid(formid);
			List<WmFormRoom> bindedRoomList = formService.getFormRoomListByFormid(formid);
			for(WmFormRoom wfr : bindedRoomList){
				WmRoom room = roomService.getRoomsByUuid(wfr.getRoomid());
				room.setDoortemplate("");
				roomService.updateByPrimaryKey(room);
			}
			formService.unbindAllFormRoom(formid);
			formService.deleteForm(form);

			
			
			mp.put("next", true);
			return APIResultFactory.success(mp);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			mp.put("next", false);
			return APIResultFactory.fail();
		}
	}
	
	@RequestMapping(value="/form/{formid}/update",method=RequestMethod.POST)
	public Object form_update(@PathVariable String formid, @RequestBody String body) {
		JsonNode jsonNode;
		Map<String, Object> mp = new HashMap<String, Object>();
		try {
			jsonNode = JSONUtils.toJson(body);
			
//			ArrayNode array = jsonNode.get("record").;
			
			String name = jsonNode.get("name").asText();
			
			List<String> strlist = new ArrayList<String>();
			int size = jsonNode.get("rooms").size();
			for(int i=0;i< size;i++){
				JsonNode el = jsonNode.get("rooms").get(i);
				strlist.add(el.get("roomuuid").asText());
			}
			
			//////////////////////
			List<String> strfieldsList = new ArrayList<String>();
			int fieldSize = jsonNode.get("fields").size();
			for(int i =0; i< fieldSize; i++){
				JsonNode el = jsonNode.get("fields").get(i);
				strfieldsList.add(el.get("fieldname").asText());
			}
			//fields 处理
			List<WmFormField> orgFieldslist = formService.getFormFieldListByFormid(formid);
			int temp_i = 0;
			for(String fieldsStr: strfieldsList){
				
				boolean existIt = false;
				
				for(WmFormField item : orgFieldslist){
					if(item.getName().equals(fieldsStr)){
						existIt = true;
					}
				}
				
				if(!existIt){
					temp_i++;
					WmFormField newField = new WmFormField();
					newField.setFieldid(UUID.randomUUID().toString());
					newField.setFieldtype("text");
					newField.setFormid(formid);
					newField.setName(fieldsStr);
					newField.setTime(new Date(System.currentTimeMillis()));
					newField.setOrdernum(temp_i + orgFieldslist.size());
					formService.saveFormField(newField);
				}
			}
			///////////////////////////
			
			WmForm form = formService.getFormByFormid(formid);
			
			if(!form.getName().equals(name)){
				form.setName(name);
				formService.updateForm(form);
			}
			
			List<WmFormRoom> bindedRoomList = formService.getFormRoomListByFormid(formid);
			for(WmFormRoom wfr : bindedRoomList){
				WmRoom room = roomService.getRoomsByUuid(wfr.getRoomid());
				room.setDoortemplate("");
				roomService.updateByPrimaryKey(room);
			}
			formService.unbindAllFormRoom(formid);
			if(strlist != null && !strlist.isEmpty()){
				for(String roomuuid : strlist){
					WmRoom room = roomService.getRoomsByUuid(roomuuid);
					room.setDoortemplate("regist");
					roomService.updateByPrimaryKey(room);
					formService.bindFormRoomById(formid, roomuuid);					
				}

			}
			mp.put("next", true);
			return APIResultFactory.success(mp);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			mp.put("next", false);
			return APIResultFactory.fail();
		}
	}
	
//	post: /luyan/form/create
//	body:
//	{
//	name:xxx,
//	rooms:[{roomuuid: roomid1},{roomuuid:roomid2},{roomuuid:roomid3}]
//	}
	@RequestMapping(value="/form/create",method=RequestMethod.POST)
	public Object form_create(@RequestBody String body) {
		JsonNode jsonNode;
		Map<String, Object> mp = new HashMap<String, Object>();
		try {
			jsonNode = JSONUtils.toJson(body);
			
//			ArrayNode array = jsonNode.get("record").;
			
			String formid = UUID.randomUUID().toString();
			String name = jsonNode.get("name").asText();
			
			List<String> strlist = new ArrayList<String>();
			int size = jsonNode.get("rooms").size();
			for(int i=0;i< size;i++){
				JsonNode el = jsonNode.get("rooms").get(i);
				strlist.add(el.get("roomuuid").asText());
			}
			
			
			//////////////////////
			List<String> strfieldsList = new ArrayList<String>();
			int fieldSize = jsonNode.get("fields").size();
			for(int i =0; i< fieldSize; i++){
				JsonNode el = jsonNode.get("fields").get(i);
				strfieldsList.add(el.get("fieldname").asText());
			}
			//fields 处理
			List<WmFormField> orgFieldslist = formService.getFormFieldListByFormid(formid);
			int temp_i = 0;
			for(String fieldsStr: strfieldsList){
				
				boolean existIt = false;
				
				for(WmFormField item : orgFieldslist){
					if(item.getName().equals(fieldsStr)){
						existIt = true;
					}
				}
				
				if(!existIt){
					temp_i++;
					WmFormField newField = new WmFormField();
					newField.setFieldid(UUID.randomUUID().toString());
					newField.setFieldtype("text");
					newField.setFormid(formid);
					newField.setName(fieldsStr);
					newField.setTime(new Date(System.currentTimeMillis()));
					newField.setOrdernum(temp_i + orgFieldslist.size());
					formService.saveFormField(newField);
				}
			}
			///////////////////////////
			
			WmForm form = new WmForm();
			form.setFormid(formid);
			form.setName(name);
			form.setTime(new Date(System.currentTimeMillis()));
			
			formService.saveForm(form);
			
			if(strlist != null && !strlist.isEmpty()){
				for(String roomuuid : strlist){
					WmRoom room = roomService.getRoomsByUuid(roomuuid);
					room.setDoortemplate("regist");
					roomService.updateByPrimaryKey(room);
					formService.bindFormRoomById(formid, roomuuid);					
				}

			}
			mp.put("next", true);
			return APIResultFactory.success(mp);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			mp.put("next", false);
			return APIResultFactory.fail();
		}
	}
	
}
